using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiltronASP.Models;
using FiltronASP.Models.DAO.WIX;
using FiltronASP.Models.DAO.Extensions;
using FiltronASP.Models.DAO.Interfaces.WIX;
using FiltronASP.Models.DAO.Filtron;
using FiltronASP.Models.DAO.Interfaces.Filtron;


namespace FiltronASP.Helpers.AjaxModel
{
    /// <summary>
    /// Klasa pomocnicza do generowania stringa z lista modeli.
    /// </summary>
    public static class AutoFillHelper
    {

        /// <summary>
        /// Metoda generująca JSonowy
        /// </summary>
        /// <param name="givenString">Część stringa do wygenerowania.</param>
        /// <returns>JSON'owy obiekt z:
        /// query - wpisana część stringa,
        /// suggestions - wygenerowane podpowiedzi
        /// </returns>
        public static string FillInputByGivenStringFiltron(string givenString)
        {
            if (givenString == String.Empty)
            {
                return String.Empty;
            }
            string resultToReturn = String.Empty;
            // JSON'owa część do zwrócenia
            string queryToReturn = String.Empty;
            string suggestionsToReturn = String.Empty;
            queryToReturn = givenString;

            IWixInterchangeDAO wixInterchangeDAO = new WixInterchangeDAO();
            IQueryable<WixInterchange> interchangeCollection = wixInterchangeDAO.FindByForeignFilterName(givenString);
            if (interchangeCollection != null)
            {
                foreach (WixInterchange filter in interchangeCollection)
                {
                    if (filter != null)
                    {
                        suggestionsToReturn += "'" + filter.foreign_filter_number + "', ";
                    }
                } //feach
            }

            //Filtron
            IFiltronInterchangeDAO filtronInterchangeDAO = new FiltronInterchangeDAO();
            IQueryable<FiltronInterchange> interchangesCollectionFiltron = filtronInterchangeDAO.FindByForeignFilterName(givenString);
            if (interchangesCollectionFiltron != null)
            {
                foreach (FiltronInterchange filter in interchangesCollectionFiltron)
                {
                    if (filter != null)
                    {
                        suggestionsToReturn += "'" + filter.foreign_filter_number + "', ";
                    }
                }
            }

            resultToReturn = "{ query:'" + queryToReturn + "',suggestions:[" + suggestionsToReturn + "]}";

            return resultToReturn;
        }

        public static string FillInputByGivenStringWix(string givenString)
        {
            if (givenString == String.Empty)
            {
                return String.Empty;
            }
            string resultToReturn = String.Empty;
            // JSON'owa część do zwrócenia
            string queryToReturn = String.Empty;
            string suggestionsToReturn = String.Empty;
            queryToReturn = givenString;

            IWixInterchangeDAO wixInterchangeDAO = new WixInterchangeDAO();
            IQueryable<WixInterchange> interchangeCollection = wixInterchangeDAO.FindByForeignFilterName(givenString);
            if (interchangeCollection != null)
            {
                foreach (WixInterchange filter in interchangeCollection)
                {
                    if (filter != null)
                    {
                        suggestionsToReturn += "'" + filter.foreign_filter_number + "', ";
                    }
                } //feach
            }

            //Filtron
            IFiltronInterchangeDAO filtronInterchangeDAO = new FiltronInterchangeDAO();
            IQueryable<FiltronInterchange> interchangesCollectionFiltron = filtronInterchangeDAO.FindByForeignFilterName(givenString);
            if (interchangesCollectionFiltron != null)
            {
                foreach (FiltronInterchange filter in interchangesCollectionFiltron)
                {
                    if (filter != null)
                    {
                        suggestionsToReturn += "'" + filter.foreign_filter_number + "', ";
                    }
                }
            }

            resultToReturn = "{ query:'" + queryToReturn + "',suggestions:[" + suggestionsToReturn + "]}";

            return resultToReturn;
        }
    }
}
